<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of pager
 *
 * @author Administrator
 */
class Page {

    protected $total;     //查询所有的数据总记录数
    protected $page;      //当前第几页
    protected $num;       //每页显示记录的条数
    protected $pageNum;   //一共多少页
    protected $url;
    protected $nextPage;
    protected $prevPage;
    protected $start;
    protected $end;
    protected $first = '第一页';//'<img style=\'border=0;margin:0;padding:0;width:8px;height:8px;\' src="../template/images/first.gif">';
    protected $last = '最后页';//<img style=\'border=0;margin:0;padding:0;width:8px;height:8px;\' src="../template/images/last.gif">';
    protected $next = '下一页';//'<img style=\'border=0;margin:0;padding:0;width:8px;height:8px;\' src="../template/images/next.gif">';
    protected $prev = '上一页';//'<img style=\'border=0;margin:0;padding:0;width:8px;height:8px;\' src="../template/images/prev.gif">';

    function __construct($url, $total, $num=5) {
        $this->page = isset($_GET["page"]) ? $_GET["page"] : 1;
        $this->url = $url;
        $this->total = $total;
        $this->num = $num;
        $this->pageNum = $this->getPageNum();
        $this->nextPage = $this->getNextPage();
        $this->prevPage = $this->getPrevPage();
        $this->start = $this->getStartNum();
        $this->end = $this->getEndNum();
    }

    protected function getPageNum() {
        return ceil($this->total / $this->num);
    }

    protected function getNextPage() {
        if ($this->page == $this->pageNum)
            return false;
        else
            return $this->page + 1;
    }

    protected function getPrevPage() {
        if ($this->page == 1)
            return false;
        else
            return $this->page - 1;
    }

    //数据库查询的偏移量
    public function getOffset() {
        return ($this->page - 1) * $this->num;
    }

    //当前页开始的记录数
    protected function getStartNum() {
        if ($this->total == 0)
            return 0;
        else
            return $this->getOffset() + 1;
    }

    //当前页结束的记录数
    protected function getEndNum() {
        return min($this->getOffset() + $this->num, $this->total);
    }

    public function getPage() {
        $pinfo="<div style='float:left;height:25px;line-height:25px;'>共 <b>{$this->total}</b> 条记录,本页显示 {$this->start}-{$this->end} 条&nbsp;&nbsp;&nbsp;&nbsp;{$this->page}/{$this->pageNum}</div>";
        if ($this->page == 1)
            $pinfo.='<span style=\'float:left;height:25px;line-height:25px;\'>'.$this->first . '&nbsp;&nbsp</span>';
        else
            $pinfo.='<a style=\'margin-right:5px;padding:0;width:40px;background:#ccc;float:left;height:25px;line-height:25px;\' href="'.$this->url.'page=1">'.$this->first . '</a>';


        if ($this->prevPage)
            $pinfo.='<a style=\'margin-right:5px;padding:0;width:40px;background:#ccc;height:25px;line-height:25px;float:left;\' href="'.$this->url.'page='.$this->prevPage . '">' . $this->prev . '</a>';
        else
        $pinfo.='<span style=\'float:left;height:25px;line-height:25px;\'>'.$this->prev . '&nbsp;&nbsp;</span>';

        if ($this->nextPage)
            $pinfo.='<a style=\'margin-right:5px;padding:0;width:40px;background:#ccc;height:25px;line-height:25px;float:left;\' href="'.$this->url.'page='.$this->nextPage . '">' . $this->next . '</a>';
        else
        $pinfo.='<span style=\'float:left;height:25px;line-height:25px;\'>'.$this->next . '&nbsp;&nbsp;</span>';

        if ($this->page == $this->pageNum)
            $pinfo.='<span style=\'float:left;height:25px;line-height:25px;\'>'.$this->last . '&nbsp;&nbsp;</span>';
        else
            $pinfo.='<a style=\'margin-right:5px;padding:0;width:40px;background:#ccc;float:left;height:25px;line-height:25px;\' href=\''.$this->url.'page='.$this->pageNum . '\'>' . $this->last . '</a>';

        return $pinfo;
    }

}